var usermanage = []
var tbodys = document.querySelector("tbody")
var pageSize = 5; //每页显示的记录条数
var page = 1; //当前页，默认第一页

let pag = {
    pageindex: page,
    pagenum: pageSize
}
ajaxHttp("post", "user/findUserList", pag).then((res) => {
    res = JSON.parse(res)
    var data = res.dataset.userList
    usermanage = data
    var pae = res.dataset.pagecount
    //    console.log(pae);
    var spantotal = document.querySelector("#spanTotalPage")
    spantotal.innerText = pae
    var spanpag = document.querySelector("#spanPageNum")
    spanpag.innerText = page
    for (var i = 0; i < usermanage.length; i++) {
        var newtr = document.createElement("tr")
        newtr.innerHTML = `
             <td><input type="checkbox" name="" id="che"></td>
             <td>${usermanage[i].userName}</td>
                            <td>${usermanage[i].userDept}</td>
                            <td>${usermanage[i].userGender}</td>
                            <td>${usermanage[i].userPhone}</td>
                            <td>${usermanage[i].userRole}</td>
                            <td class="operate">
                            <button class="revise" onclick="revise(${usermanage[i].userId})" style="color:#1BB27F; cursor:pointer;">修改</button>
                            <button onclick="deltr(${usermanage[i].userId})" style="color:#E86A73;cursor:pointer;">删除</button>
                        </td>
        `
        tbodys.appendChild(newtr)
    }
    // pages()

})


// 查找用户
function searchuser() { 
    var ser=document.querySelector("#nauser")
    let sear={
        userName:ser.value,
        pageindex:page,
        pagenum:pageSize
    }
    ajaxHttp("POST","user/findUserList",sear).then((res)=>{
        // console.log(res);
        res=JSON.parse(res)
        // console.log(res);
        var data = res.dataset.userList
        usermanage = data
        var pae = res.dataset.pagecount
        //    console.log(pae);
        var spantotal = document.querySelector("#spanTotalPage")
        spantotal.innerText = pae
        var spanpag = document.querySelector("#spanPageNum")
        spanpag.innerText = page
        tbodys.innerHTML=""
        for (var i = 0; i < usermanage.length; i++) {
            var newtr = document.createElement("tr")
            newtr.innerHTML = `
                 <td><input type="checkbox" name="" id="che"></td>
                 <td>${usermanage[i].userName}</td>
                                <td>${usermanage[i].userDept}</td>
                                <td>${usermanage[i].userGender}</td>
                                <td>${usermanage[i].userPhone}</td>
                                <td>${usermanage[i].userRole}</td>
                                <td class="operate">
                                <button class="revise" onclick="revise(${usermanage[i].userId})" style="color:#1BB27F; cursor:pointer;">修改</button>
                                <button onclick="deltr(${usermanage[i].userId})" style="color:#E86A73;cursor:pointer;">删除</button>
                            </td>
            `
            tbodys.appendChild(newtr)
        }
    })
}


// 随机生成账号
var accounts = document.querySelector("#account")
function account() {
    var num = ""//存放用户账号
    for (var i = 0; i < 2; i++) {
        num += Math.floor(Math.random() * 10)
    }
    var time = new Date()
    var y = time.getFullYear()
    var m = time.getMonth() + 1
    var data = time.getDate()
    // num=y+num
    num = "BR" + y.toString() + m.toString() + data.toString() + num
    accounts.value = num
    return num
}
// console.log(new Date().getTime());
var opens = document.querySelector(".open")
// 单击修改打开修改弹窗
function revise(ise) {
    console.log(ise);
    var names = opens.querySelector(".names")
    var sex = opens.querySelector("#sex")
    var acc = opens.querySelector("#account")
    var pwd = opens.querySelector(".pwd")
    var clazz = opens.querySelector(".clazz")
    var role = opens.querySelector(".role")
    var tel = opens.querySelector(".tel")
    var data=this.usermanage
    for(var i=0;i<data.length;i++){
        if(ise===data[i].userId){
            console.log(data[i]);
            names.value=data[i].userName    
            sex.value=data[i].userGender
            acc.value=data[i].userAccount
            pwd.value=data[i].userPassword
            clazz.value=data[i].userDept
            role.value=data[i].userRole
            tel.value=data[i].userPhone
            // console.log(tel);
        }
    }
    opens.style.display = "flex"
    localStorage.setItem("userId",ise)
}
// 修改接口
function userrevise(){
    var open=document.querySelector(".open")
    var names = open.querySelector(".names").value
    var sex = open.querySelector("#sex").value
    var pwd = open.querySelector(".pwd").value
    var clazz = open.querySelector(".clazz").value
    var role = open.querySelector(".role").value//传参的时候传递数字
    var tel = open.querySelector(".tel").value
    // console.log(this);
    var index=localStorage.getItem("userId")
    var num=Number(index)
    let userre={
        userPassword:pwd,
        userId:num,
        userName:names,
        userRole:Number(role),
        userGender:sex,
        userDept:clazz,
        userPhone:tel
    }
    chec()
    ajaxHttp("post","user/updateUserInfo",userre).then((res)=>{
        // console.log(res);
        alert("修改成功")
        offrev()
        location.reload()
    })
}

// 单击修改取消关闭遮罩
function offrev() {
    opens.style.display = "none"
}
// 删除功能
var dels=document.querySelector(".del")
function deltr(dele) {
    // 调用删除接口
    dels.style.display="flex"
    localStorage.setItem("userId",dele)
}
// 调用删除接口
function deluser(){
    var index=localStorage.getItem("userId")
    var num=Number(index)
    let delus={
        userId:num
    }
    ajaxHttp("post","user/deleteUser",delus).then((res)=>{
        alert("删除成功")
        offdel()
        location.reload()
    })
}
// 关闭删除弹窗
function offdel(){
    dels.style.display="none"
}


// 打开添加用户---报错500
var addopens = document.querySelector(".addopen")
function addopen() {
    addopens.style.display = "flex"
}
function adduser() {
    var names = document.querySelector(".names").value
    var sex = document.querySelector("#sex").value
    var acc = document.querySelector("#account").value
    var pwd = document.querySelector(".pwd").value
    var clazz = document.querySelector(".clazz").value
    var role = document.querySelector(".role").value
    var tel = document.querySelector(".tel").value
   
    let users = {
        userName: names,
        userGender: sex,
        userAccount: acc,
        userPassword: pwd,
        userDept: Number(clazz),
        userRole: Number(role),
        userPhone: tel
    }
    console.log(users);
    chec()
    ajaxHttp("post", "user/addUser", users).then((res) => {
        // console.log(res);
        alert("添加成功")
        offadd()
        location.reload()
    })
}

function offadd() {
    addopens.style.display = "none"
}
// 账号验重

function chec(){
    var acc=document.querySelector("#account").value
    let acche={
        userAccount:account.value
    }
    ajaxHttp("post","user/checkUser",acc).then((res)=>{
        console.log(res);
    })
}
// 分页
// 下一页
function pages() {
    let pagea = {
        pageindex: page,
        pagenum: pageSize
    }
    ajaxHttp("post", "user/findUserList", pagea).then((res) => {
        res = JSON.parse(res)
        console.log(res);
        var data = res.dataset.userList
        usermanage = data
        tbodys.innerHTML = ""
        // spantotal()
        var pae = res.dataset.pagecount
        //    console.log(pae);
        var spantotal = document.querySelector("#spanTotalPage")
        spantotal.innerText = pae
        var spanpag = document.querySelector("#spanPageNum")
        spanpag.innerText = page
        if (page < pae) {
            page++;
            tbodys.innerHTML = ""
            for (var i = 0; i < usermanage.length; i++) {
                var newtr = document.createElement("tr")
                newtr.innerHTML = `
                         <td><input type="checkbox" name="" id="che"></td>
                         <td>${usermanage[i].userName}</td>
                                        <td>${usermanage[i].userDept}</td>
                                        <td>${usermanage[i].userGender}</td>
                                        <td>${usermanage[i].userPhone}</td>
                                        <td>${usermanage[i].userRole}</td>
                                        <td class="operate">
                                        <button class="revise" onclick="revise();account()" style="color:#1BB27F; cursor:pointer;">修改</button>
                                        <button onclick="deltr()" style="color:#E86A73;cursor:pointer;">删除</button>
                                    </td>
                    `
                tbodys.appendChild(newtr)
            }
        } else if (page == pae) {
            // alert("最后一页")
            tbodys.innerHTML = ""
            for (var i = 0; i < usermanage.length; i++) {
                var newtr = document.createElement("tr")
                newtr.innerHTML = `
                         <td><input type="checkbox" name="" id="che"></td>
                         <td>${usermanage[i].userName}</td>
                                        <td>${usermanage[i].userDept}</td>
                                        <td>${usermanage[i].userGender}</td>
                                        <td>${usermanage[i].userPhone}</td>
                                        <td>${usermanage[i].userRole}</td>
                                        <td class="operate">
                                        <button class="revise" onclick="revise();account()" style="color:#1BB27F; cursor:pointer;">修改</button>
                                        <button onclick="deltr()" style="color:#E86A73;cursor:pointer;">删除</button>
                                    </td>
                    `
                tbodys.appendChild(newtr)
            }
        }
    })
}
// 上一页
function uppages() {
    let pagea = {
        pageindex: page,
        pagenum: pageSize
    }
    ajaxHttp("post", "user/findUserList", pagea).then((res) => {
        res = JSON.parse(res)
        console.log(res);
        var data = res.dataset.userList
        usermanage = data
        tbodys.innerHTML = ""
        // spantotal()
        var pae = res.dataset.pagecount
        //    console.log(pae);
        var spantotal = document.querySelector("#spanTotalPage")
        spantotal.innerText = pae
        var spanpag = document.querySelector("#spanPageNum")
        spanpag.innerText = page
        if (page > 1) {
            page--;
            tbodys.innerHTML = ""
            for (var i = 0; i < usermanage.length; i++) {
                var newtr = document.createElement("tr")
                newtr.innerHTML = `
                         <td><input type="checkbox" name="" id="che"></td>
                         <td>${usermanage[i].userName}</td>
                                        <td>${usermanage[i].userDept}</td>
                                        <td>${usermanage[i].userGender}</td>
                                        <td>${usermanage[i].userPhone}</td>
                                        <td>${usermanage[i].userRole}</td>
                                        <td class="operate">
                                        <button class="revise" onclick="revise();account()" style="color:#1BB27F; cursor:pointer;">修改</button>
                                        <button onclick="deltr()" style="color:#E86A73;cursor:pointer;">删除</button>
                                    </td>
                    `
                tbodys.appendChild(newtr)
            }
        } else if (page == 1) {
            // alert("最后一页")
            tbodys.innerHTML = ""
            for (var i = 0; i < usermanage.length; i++) {
                var newtr = document.createElement("tr")
                newtr.innerHTML = `
                         <td><input type="checkbox" name="" id="che"></td>
                         <td>${usermanage[i].userName}</td>
                                        <td>${usermanage[i].userDept}</td>
                                        <td>${usermanage[i].userGender}</td>
                                        <td>${usermanage[i].userPhone}</td>
                                        <td>${usermanage[i].userRole}</td>
                                        <td class="operate">
                                        <button class="revise" onclick="revise();account()" style="color:#1BB27F; cursor:pointer;">修改</button>
                                        <button onclick="deltr()" style="color:#E86A73;cursor:pointer;">删除</button>
                                    </td>
                    `
                tbodys.appendChild(newtr)
            }
        }
    })
}